package com.ds.home.database.dao;

import com.ds.common.JDSConstants;
import com.ds.common.logging.Log;
import com.ds.common.logging.LogFactory;
import com.ds.home.database.BaseHibernateDAO;
import com.ds.home.database.vo.PlaceVO;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.query.NativeQuery;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for PlaceVO entities. Transaction control of the
 * save(), update() and delete() operations can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.ds.home.database.vo.PlaceVO
 * @author MyEclipse Persistence Tools
 */
public class PlaceDAO extends BaseHibernateDAO {
    private static final Log log = LogFactory.getLog(JDSConstants.CONFIG_KEY, PlaceDAO.class);
    // property constants
    public static final String NAME = "name";
    public static final String USERID = "userid";
    public static final String MEMO = "memo";
    public static final String ORGID = "orgid";
    public static final String PARENTID = "parentid";

    public void update(PlaceVO transientInstance) {
	log.debug("update PlaceVO instance");
	try {
	    getSession().getTransaction().begin();
	    getSession().update(transientInstance);
	    getSession().getTransaction().commit();
	    log.debug("update successful");
	} catch (RuntimeException re) {
	    log.error("update failed", re);
	    throw re;
	} finally {
	    close();
	}
    }


	public  List<PlaceVO> loadByWhere(String where ){

		NativeQuery<PlaceVO> query = this.getSession().createNativeQuery("select * from ha_place "+where, PlaceVO.class);
		List<PlaceVO>  volist= query.getResultList();

		return volist;
	}

    public void delete(PlaceVO persistentInstance) {
	log.debug("deleting PlaceVO instance");
	try {
	    getSession().getTransaction().begin();
	    getSession().delete(persistentInstance);
	    getSession().getTransaction().commit();
	    log.debug("delete successful");
	} catch (RuntimeException re) {
	    log.error("delete failed", re);
	    throw re;
	} finally {
	    close();
	}
    }

    public PlaceVO findById(String id) {
	log.debug("getting PlaceVO instance with id: " + id);
	PlaceVO instance = null;
	try {
	    instance = (PlaceVO) getSession().get("com.ds.home.database.vo.PlaceVO", id);
	} catch (RuntimeException re) {
	    log.error("get failed", re);
	    throw re;
	} finally {
	    close();
	}
	return instance;
    }

    public List<PlaceVO> findByExample(PlaceVO instance) {
	log.debug("finding PlaceVO instance by example");
	List results = null;
	try {
	    results = getSession().createCriteria("com.ds.home.database.vo.PlaceVO").add(Example.create(instance)).list();
	    log.debug("find by example successful, result size: " + results.size());

	} catch (RuntimeException re) {
	    log.error("find by example failed", re);
	    throw re;
	} finally {
	    close();
	}
	return results;
    }

    public List<PlaceVO> findByProperty(String propertyName, Object value) {
	log.debug("finding PlaceVO instance with property: " + propertyName + ", value: " + value);
	List results = null;
	try {
	    String queryString = "from PlaceVO as model where model." + propertyName + "= ?0";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setParameter(0, value);
	    results = queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find by property name failed", re);
	    throw re;
	} finally {
	    close();
	}
	return results;
    }

    public List<PlaceVO> findByName(Object name) {
	return findByProperty(NAME, name);
    }

    public List<PlaceVO> findByUserid(Object userid) {
	return findByProperty(USERID, userid);
    }

    public List<PlaceVO> findByOrgid(Object orgid) {
	return findByProperty(ORGID, orgid);
    }

    public List<PlaceVO> findByParentid(Object parentid) {
	return findByProperty(PARENTID, parentid);
    }

    public List<PlaceVO> findPlaceNotNull(String propertyName) {

	log.debug("finding PlaceVO instance with orgId not null");
	List results = null;
	try {
	    String queryString = "from PlaceVO as model where model." + propertyName + " is not null ";
	    Query queryObject = getSession().createQuery(queryString);

	    results = queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find by property name failed", re);
	    throw re;
	} finally {
	    close();
	}
	return results;

    }

    public List<PlaceVO> findAllOrgPlace() {

	return findPlaceNotNull(ORGID);

    }

    public List<PlaceVO> findAllUserPlace() {
	return findPlaceNotNull(USERID);
    }

    public List<PlaceVO> findByMemo(Object memo) {
	return findByProperty(MEMO, memo);
    }

    public List<PlaceVO> findAll() {
	log.debug("finding all PlaceVO instances");
	List results = null;

	try {
	    String queryString = "from PlaceVO";
	    Query queryObject = getSession().createQuery(queryString);
	    results = queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find all failed", re);
	    throw re;
	} finally {
	    close();
	}
	return results;
    }

    public void insert(PlaceVO instance) {
	synchronized (instance.getPlaceid().intern()) {
	    log.debug("insert dirty PlaceVO instance");
	    try {
		getSession().getTransaction().begin();
		getSession().insert(instance);
		getSession().getTransaction().commit();
		log.debug("insert successful");
	    } catch (RuntimeException re) {
		log.error("insert failed", re);
		throw re;
	    } finally {
		close();
	    }
	}

    }

}